package kr.hkit.powerjava.chapter25.chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class QuizClient {
	public static void main(String[] args) throws IOException {
		
		Socket quizSocket = null;
		PrintWriter out = null;
		BufferedReader in = null;
		BufferedReader user = null; 
		
		String serverMsg;
		String sendMsg;
		
		try {
			quizSocket = new Socket("localhost", 5555);
			out = new PrintWriter(quizSocket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(quizSocket.getInputStream()));
			user = new BufferedReader(new InputStreamReader(System.in));
			
		} catch (UnknownHostException e) {
			System.err.println("localhost에접근할 수 없습니다.");
			System.exit(-1);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		while ((serverMsg = in.readLine()) != null) {
			System.out.println("서버 : " + serverMsg);
			if (serverMsg.equals("quit"))
				break;
			
			sendMsg = user.readLine();
			if (sendMsg != null)	{
				System.out.println("클라이언트: " + sendMsg);
				out.println(sendMsg);
			}
		}
		
		out.close();
		in.close();
		quizSocket.close();
	}
}
